package com.htfc786.reminder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htfc786.reminder.common.ErrorCode;
import com.htfc786.reminder.exception.BusinessException;
import com.htfc786.reminder.mapper.FamilyMemberMapper;
import com.htfc786.reminder.mapper.UserMapper;
import com.htfc786.reminder.model.entity.Family;
import com.htfc786.reminder.model.entity.FamilyMember;
import com.htfc786.reminder.model.entity.User;
import com.htfc786.reminder.service.FamilyService;
import com.htfc786.reminder.mapper.FamilyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author htfc786
* @description 针对表【family(家庭表)】的数据库操作Service实现
* @createDate 2024-10-03 21:39:52
*/
@Service
public class FamilyServiceImpl extends ServiceImpl<FamilyMapper, Family> implements FamilyService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private FamilyMapper familyMapper;

    @Resource
    private FamilyMemberMapper familyMemberMapper;

    @Override
    @Transactional
    public Family createFamily(String name, Long creatUserId) {
        // 如果已经有就不能创建
        User user = userMapper.selectById(creatUserId);
        if (user.getFamilyId() != null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "已经有家庭，不能创建");
        }
        // 创建
        Family family = new Family();
        family.setName(name);
        family.setCreatUserId(creatUserId);
        familyMapper.insert(family);
        // 把创建者加入家庭
        user.setFamilyId(family.getId());
        userMapper.updateById(user);
        // 创建家庭成员
        addFamilyMember(family.getId(), user.getUserName(), user.getId(), true, true);
        return family;
    }

    @Override
    public Family updateFamilyName(Long id, String name) {
        Family family = familyMapper.selectById(id);
        if (family != null) {
            family.setName(name);
            familyMapper.updateById(family);
        }
        return family;
    }

    @Override
    public Family getFamilyById(Long id) {
        return familyMapper.selectById(id);
    }

    @Override
    public List<Family> searchFamiliesByName(String name) {
        QueryWrapper<Family> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return familyMapper.selectList(queryWrapper);
    }

    @Override
    public FamilyMember addFamilyMember(Long familyId, String name, Long userId, Boolean receiveLowStockNotifications, Boolean receiveUsageReminders) {
        FamilyMember familyMember = new FamilyMember();
        familyMember.setFamilyId(familyId);
        familyMember.setName(name);
        familyMember.setUserId(userId);
        familyMember.setReceiveLowStockNotifications(receiveLowStockNotifications);
        familyMember.setReceiveUsageReminders(receiveUsageReminders);
        familyMemberMapper.insert(familyMember);
        return familyMember;
    }

    @Override
    public FamilyMember addFamilyMember(Long familyId, String name) {
        FamilyMember familyMember = new FamilyMember();
        familyMember.setFamilyId(familyId);
        familyMember.setName(name);
        familyMemberMapper.insert(familyMember);
        return familyMember;
    }

    @Override
    public int deleteFamilyMember(Long familyMemberId, Long currentUserId) {
        // 获取家庭成员信息
        FamilyMember familyMember = familyMemberMapper.selectById(familyMemberId);
        FamilyMember currentUserMember = familyMemberMapper.selectById(currentUserId);
        if (familyMember == null || currentUserMember == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "家庭成员不存在");
        }
        if (!familyMember.getFamilyId().equals(currentUserMember.getFamilyId())) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权限删除");
        }
        // 删除家庭成员
        return familyMemberMapper.deleteById(familyMemberId);
    }


    @Override
    public FamilyMember updateFamilyMember(FamilyMember familyMember, Long currentUserId) {
        // 获取家庭成员信息
        FamilyMember currentUserMember = familyMemberMapper.selectById(currentUserId);
        if (currentUserMember == null || familyMember == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "家庭成员不存在");
        }
        if (!currentUserMember.getFamilyId().equals(familyMember.getFamilyId())) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权限删除");
        }
        familyMemberMapper.updateById(familyMember);
        return familyMember;
    }

    @Override
    public List<FamilyMember> getAllFamilyMembers(Long familyId) {
        QueryWrapper<FamilyMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("familyId", familyId);
        return familyMemberMapper.selectList(queryWrapper);
    }
}




